<?php
namespace app\front\service\v2;

use app\front\service\GetMsgService;

class ResposeMsg extends GetMsgService
{
    public function responseMsg(){
        //根据用户传过来的消息类型进行不同的响应
        //1、接收微信服务器POST过来的数据，XML数据包
        //     请使用file_get_contents('php://input')代替$HTTP_RAW_POST_DATA; 因为PHP 5.6开始$HTTP_RAW_POST_DATA已经被DEPRECATE了。
        $postData = file_get_contents('php://input');
        
        if(!$postData)
        {
            echo  "error";
            exit();
        }
        
        //2、解析XML数据包
        
        $object = simplexml_load_string($postData,"SimpleXMLElement",LIBXML_NOCDATA);
        
        //获取消息类型
        $MsgType = $object->MsgType;
        switch ($MsgType) {
            case 'event':
                //接收事件推送
                $this->receiveEvent($object);
                break;
            case 'text':
                //接收文本消息
                 //  echo $this->receiveText($object);
                break;
            case 'image':
                //接收图片消息
               
               echo $this->receiveImage($object);
                break;
            case 'location':
                //接收地理位置消息
              //  echo $this->receiveLocation($object);
                break;
            case 'voice':
                //接收语音消息
               // echo $this->receiveVoice($object);
                break;
            case 'video':
                //接收视频消息
               // echo $this->receiveVideo($object);
                break;
            case  'link':
                //接收链接消息
              //  echo $this->receiveLink($object);
                break;
            default:
                break;
        }
    }
   
    public function receiveEvent($obj){
        switch ($obj->Event) {
            //关注事件
            case 'subscribe':
                //扫描带参数的二维码，用户未关注时，进行关注后的事件
                if(!empty($obj->EventKey)){
                    //做相关处理                  
                }
                //判断用户opid是否已存在
              
                if( $this->existUserinfo($obj->FromUserName))
                {
                    $this->updateUserInfo($obj->FromUserName);
                }else{
                    
                    $this->saveUserInfo($obj->FromUserName);
                }
                // echo $this->receiveImage($obj);
                  echo $this->replyText($obj,"欢迎关注至富通！");
             
                //   echo $this->replyText($obj,$res);
                //
                /*
                 $dataArray = array(
                 array(
                 "Title"=>"国庆乐疯 大奖抽疯大转盘！",
                 "Description"=>"国庆乐疯，大奖抽疯，赢取心动大奖！",
                 "PicUrl"=>"http://yunkt.qiniudn.com/wxdzp_bg.jpg",
                 "Url"=>"http://2.lampym.vipsinaapp.com/index.php/bigwheel"
                 )
                 );
                 echo $this->replyNews($obj,$dataArray);
                 */
                break;
                //取消关注事件
            case 'unsubscribe':
                break;
                //扫描带参数的二维码，用户已关注时，进行关注后的事件
            case 'SCAN':
                //做相关的处理
                break;
                //自定义菜单事件
            case 'CLICK':
                //
                switch ($obj->EventKey)
                {
                    case 'GOUYIN':
                        echo $this->replyText($obj,"TEL：自定义");
                        break;
                    case 'VIDEO':
                        echo $this->replyText($obj,"自定义
");
                        break;
                    case 'CODE':
                        echo $this->replyText($obj,"自定义
");
                        break;
                    default:
                        echo $this->replyText($obj,"自定义。");
                        break;
                }
                break;
        }
    }
    
    //接收文本消息
    public function receiveText($obj){
        //获取文本消息的内容
        $content = $obj->Content;
        // return $this->replyText($obj,$content);
        
        
        //调用图灵机器人接口
//         $tulingUrl = "http://www.tuling123.com/openapi/api?key=e0b26c08c28d372253a96c7f56b86b41&info={$content}";
//         $replyContent = $this->https_request($tulingUrl);
        return $this->replyText($obj, $content);
        
    }
    
    //接收图片消息
    public function receiveImage($obj)
    {
        logs("OBJECT.TXT", $obj);
        //获取图片消息的内容
        $imageArr = array(
            "PicUrl"=>'http://mmbiz.qpic.cn/mmbiz_jpg/qm2BgoMiaIQFdyiaTM4MDdG6WxlYPgiayUnjOTKSjibYyBuVfdTpwns3NMP8kvd4oQoONP0wysC5mcqx23nCOcpyXg/0',
            "MediaId"=>'uA-9903lBlZT-_zcBiIFgnnIMsf07HbCLKXu_XOi-vBDI0s8yW0HOBQXaL_N4hwF'
        );
//         $imageArr = array(
//             "PicUrl"=>$obj->PicUrl,
//             "MediaId"=>$obj->MediaId
//         );
        logs("IMG.TXT", $imageArr);
        //发送图片消息
        return $this->replyImage($obj,$imageArr);
    }
    
    //接收地理位置消息
    public function receiveLocation($obj)
    {
        //获取地理位置消息的内容
        $locationArr = array(
            "Location_X"=>$obj->Location_X,
            "Location_Y"=>"地址位置经度：".$obj->Location_Y,
            "Label"=>$obj->Label
        );
        //回复文本消息
        return $this->replyText($obj,$locationArr['Location_Y']);
    }
    
    //接收语言消息
    public function receiveVoice($obj){
        //获取语言消息内容
        $voiceArr = array(
            "MediaId"=>$obj->MediaId,
            "Format"=>$obj->Format
        );
        //回复语言消息
        return $this->replyVoice($obj,$voiceArr);
    }
    
    //接收视频消息
    public function receiveVideo($obj){
        //获取视频消息的内容
        $videoArr = array(
            "MediaId"=>$obj->MediaId
        );
        //回复视频消息
        return $this->replyVideo($obj,$videoArr);
    }
    
    //接收链接消息
    public function receiveLink($obj)
    {
        //接收链接消息的内容
        $linkArr = array(
            "Title"=>$obj->Title,
            "Description"=>$obj->Description,
            "Url"=>$obj->Url
        );
        //回复文本消息
        return $this->replyText($obj,"你发过来的链接地址是{$linkArr['Url']}");
    }
    
    //发送文本消息
    public function replyText($obj,$content){
        $replyXml = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[text]]></MsgType>
						<Content><![CDATA[%s]]></Content>
						</xml>";
        
//         switch (trim($content)){
//             case "初露": $content='cl 我爱你！';$flag=false; break;
//             case "tel": $content='18745132886';$flag=false; break;
//             case "慕课": $content="<a href='https://www.imooc.com'>慕课网</a>";$flag=false; break;
//         }
        //             if($flag==true){
        
        //                  //调用图灵机器人接口
        //                  $tulingUrl = "http://www.tuling123.com/openapi/api?key=e0b26c08c28d372253a96c7f56b86b41&info={$content}";
        //                  $content = $this->https_request($tulingUrl);
        
        //             }
        
        
        $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$content);
        return $resultStr;
    }
    
    //发送图片消息
    public function replyImage($obj,$imageArr){
        
        
        $toUser=$obj->FromUserName;
        $fromUser=$obj->ToUserName;
//         $arr =array(
//             //注意多图文图片数量不能超过10个
//             array(       //二维数组变成多维数组　　
//                 'title'=>'至富通',
//                 'description'=>"北京至富科技有限公司官网！",
//                 'picUrl'=>'http://www.zfshlm.com/098f6bcd4621d373cade4e832627b4f6/zfshlm_5_0/public/static/img/miandan.gif',
//                 'url'=>'http://www.zfshlm.com',
//             ),
//             array(       //二维数组变成多维数组　　
//                 'title'=>'淘宝',
//                 'description'=>"这是一个测试！",
//                 'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
//                 'url'=>'http://www.taobao.com',
//             ),
//             array(       //二维数组变成多维数组　　
//                 'title'=>'百度',
//                 'description'=>"这是一个测试！",
//                 'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
//                 'url'=>'http://www.baidu.com',
//             ),
//        );
//         $template = "<xml>
//             <ToUserName><![CDATA[%s]]></ToUserName>
//             <FromUserName><![CDATA[%s]]></FromUserName>
//             <CreateTime>%s</CreateTime>
//             <MsgType><![CDATA[%s]]></MsgType>
//             <ArticleCount>".count($arr)."</ArticleCount>
//             <Articles>";
        
//         foreach($arr as $k=>$v){
//             $template .="<item>
//                        <Title><![CDATA[".$v['title']."]]></Title>
//                        <Description><![CDATA[".$v['description']."]]></Description>
//                        <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
//                        <Url><![CDATA[".$v['url']."]]></Url>
//                        </item>";
//         }
        
        
//         $template .="</Articles>
//                    </xml> ";
        
        
        $template = "<xml>
                                    <ToUserName><![CDATA[%s]]></ToUserName>
                                    <FromUserName><![CDATA[%s]]></FromUserName>
                                    <CreateTime>%s</CreateTime>
                                    <MsgType><![CDATA[image]]></MsgType>
                                    <Image>
                                    <MediaId><![CDATA[%s]]></MediaId>
                                    </Image>
                                    </xml>";
        
        
        
        $resultStr =  sprintf($template, $toUser, $fromUser, time(), $imageArr['MediaId']);
        //返回一个进行xml数据包
        
        //$resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$imageArr);
        return $resultStr;
    }
    
    //回复语音消息
    public function replyVoice($obj,$voiceArr)
    {
        $replyXml = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[voice]]></MsgType>
						<Voice>
						<MediaId><![CDATA[%s]]></MediaId>
						</Voice>
						</xml>";
        //返回一个进行xml数据包
        
        $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$voiceArr['MediaId']);
        return $resultStr;
    }
    
    //回复视频消息
    public function replyVideo($obj,$videoArr){
        $replyXml = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[video]]></MsgType>
						<Video>
						<MediaId><![CDATA[%s]]></MediaId>
						</Video>
						</xml>";
        //返回一个进行xml数据包
        
        $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$videoArr['MediaId']);
        return $resultStr;
    }
    
    //回复音乐消息
    public function  replyMusic($obj,$musicArr)
    {
        $replyXml = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[music]]></MsgType>
						<Music>
						<Title><![CDATA[%s]]></Title>
						<Description><![CDATA[%s]]></Description>
						<MusicUrl><![CDATA[%s]]></MusicUrl>
						<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
						<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
						</Music>
						</xml>";
        //返回一个进行xml数据包
        
        $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$musicArr['Title'],$musicArr['Description'],$musicArr['MusicUrl'],$musicArr['HQMusicUrl'],$musicArr['ThumbMediaId']);
        return $resultStr;
    }
    
    //回复图文消息
    public function replyNews($obj,$newsArr){
        $itemStr = "";
        if(is_array($newsArr))
        {
            foreach($newsArr as $item)
            {
                $itemXml ="<item>
						<Title><![CDATA[%s]]></Title>
						<Description><![CDATA[%s]]></Description>
						<PicUrl><![CDATA[%s]]></PicUrl>
						<Url><![CDATA[%s]]></Url>
						</item>";
                $itemStr .= sprintf($itemXml,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
            }
            
        }
        
        $replyXml = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[news]]></MsgType>
        <ArticleCount>%s</ArticleCount>
        <Articles>
        {$itemStr}
        </Articles>
        </xml> ";
        //返回一个进行xml数据包
        
        $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),count($newsArr));
        return $resultStr;
    }
    
    
}

